package club.banyuan.oop.UserDemo;

import java.util.Arrays;

/**
 * @author edz
 * @version 1.0
 * @date 2020/11/18 上午11:13
 */
public class UserService {
    private User[] users = new User[5];

    {
        for (int i = 0; i < users.length; i++) {
            User user = new User();
            user.setUsername("张三" + i);
            user.setPassword("12345" + i);
            users[i] = user;

        }
    }

    //用户登录
    public boolean login(User user) {
        //user 对象在数据库是否存在
        for (int i = 0; i < users.length; i++) {
            if (user.getUsername().equals(users[i].getUsername())) {
                return true;
            }
        }
        return false;
    }

    //用户注册


    //用户的商品添加操作
    public void addProduct(Product product, User user) {
        Product[] products = user.getProducts();
        //扩容
        products = Arrays.copyOf(products, products.length + 1);
        products[products.length - 1] = product;
        user.setProducts(products);
    }

    //显示用户购物车里面所有的商品
    public void printInfo(User user) {
        for (int i = 0; i < user.getProducts().length; i++) {
            System.out.println(user.getProducts()[i]);
        }
    }

}
